package com.dubito.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dubito.mappers.StudentMapper;
import com.dubito.model.Student;
import com.dubito.utils.SqlSessionFactoryUtil;

public class StudentTest2 {

		private static Logger logger = Logger.getLogger(StudentTest2.class);
		private SqlSession session = null;
		private StudentMapper mapper = null;
		
		/**
		 * 先执行
		 * @throws Exception
		 */
		@Before
		public void setUp() throws Exception {
			session=SqlSessionFactoryUtil.openSession();
			
			mapper=session.getMapper(StudentMapper.class);
		}
		
		/**
		 * 后执行
		 * @throws Exception
		 */
		@After
		public void tearDown() throws Exception {
			session.close();
		}
		
		/*
		 * 添加
		 * 
		 * */
		
		@Test
		public void  add(){
			Student student = new Student("李四", 23);
			int i = mapper.add(student);
			session.commit();
			if(i > 0){
				logger.info("成功添加"+i+"条数据");
				
			}
		}
		
		/*
		 * 根据ID查询数据
		 * 
		 */
		@Test
		public void getById(){
			Student student = mapper.getById(3);
			System.out.println(student.getName());
		}
		
		/*
		 *查询全部数据
		 * 
		 */
		@Test
		public void getAll(){
			List<Student> list = mapper.getAll();
			for (Student student : list) {
				System.out.println(student.getName());
			}
		}
		/*
		 * 根据ID删除数据
		 * 
		 */
		@Test
		public void deleteById(){
			int i = mapper.deleteById(8);
			session.commit();
			if(i >0 ){
				logger.info("成功删除"+i+"条数据");
			}
		}
		
		/*
		 * 更新数据
		 */
		@Test
		public void update(){
			Student student = new Student("王道长",130);
			student.setId(7);
			int j = mapper.update(student);
			session.commit();
			if(j > 0){
				logger.info("更新"+j+"条数据");
			}
		}
		
		/*
		 * 一对一关联查询
		 * 
		 */
		@Test
		public void findStudentWithAddress(){
			Student student = mapper.findStudentWithAddress(3);
			System.out.println(student);
		}
		
		@Test
		public void findStudentWithGrade(){
			Student student = mapper.findStudentWithAddress(3);
			System.out.println(student);
		}
}
